package cn.cqie.attendance.manager.service.impl;

import cn.cqie.attendance.common.util.Page;
import cn.cqie.attendance.common.util.PageBean;
import cn.cqie.attendance.manager.service.AdminTeacherService;
import cn.cqie.attendance.manager.dao.AdminTeacherDao;
import cn.cqie.attendance.manager.pojo.AdminTeacher;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author 697
 * @date 2021/6/16 16:29
 */
@Service
public class AdminTeacherServiceImpl implements AdminTeacherService {

    @Autowired
    private AdminTeacherDao teacherDao;

    /**
     * 模糊组合查询老师信息
     * @param teacherName 老师名
     * @param jobNumber 工号
     * @param phone 手机号
     * @param status 状态0无效1有效
     * @param identity 身份证
     * @param pageSize 每一页的数量
     * @param pageNum 当前页
     * @return
     */
    @Override
    public PageBean<AdminTeacher> queryAllTeacherInfo(String teacherName, String jobNumber, String phone, String status, String identity, Integer pageSize, Integer pageNum) {

        if (pageNum == null || pageSize == null){
            pageNum = 1;
            pageSize = 10;
        }
        PageHelper.startPage(pageNum,pageSize);

        AdminTeacher teacher = new AdminTeacher();
        teacher.setTeacherName(teacherName);
        teacher.setJobNumber(jobNumber);
        teacher.setPhone(phone);
        teacher.setStatus(status);
        teacher.setIdentity(identity);
        List<AdminTeacher> list = teacherDao.queryAllTeacherInfo(teacher);

        int total = list.size();
        if(null == list || list.size() ==0){
            return new PageBean<>(201,"error",list);
        }
        /**
         * 修改   ！！！！
         * 无total参数
         */
        return new PageBean<>(200,"success",list);
    }



    /**
     * 新增老师信息
     * @param teacherName 老师名
     * @param jobNumber 工号
     * @param identity 身份证
     * @param gender 性别0女1男
     * @param phone 手机号
     * @param positionId 职位
     * @return
     */
    @Override
    public Page insertAllTeacherInfo(String teacherName, String jobNumber, String identity, Integer gender, String phone, Integer positionId) {
        AdminTeacher teacher = new AdminTeacher();
        teacher.setTeacherName(teacherName);
        teacher.setJobNumber(jobNumber);
        teacher.setIdentity(identity);
        teacher.setGender(gender);
        teacher.setPhone(phone);
        teacher.setPositionId(positionId);

        teacher.setId(null);

        //默认值
        teacher.setPassword("123456");
        teacher.setRole("0");
        teacher.setStatus("1");//默认有效
        teacher.setLoginTimes("1");
        teacher.setSubmitTime(new Date());
        //判断是否重复
        int i = teacherDao.queryCountTeacherInfoByJobNumber(teacher);
        if (i>0){
            return Page.INFOERROR;
        }else{//继续执行新增
            int msg = teacherDao.insertAllTeacherInfo(teacher);
//        System.out.println(msg);
            if (msg>0){
                return Page.SUCCESS;
            }else {
                return Page.FAIL;
            }
        }
    }


    /**
     * 根据老师工号查询老师数量
     * @param adminTeacher
     * @return
     */
    @Override
    public int queryCountTeacherInfoByJobNumber(AdminTeacher adminTeacher) {
        int i = teacherDao.queryCountTeacherInfoByJobNumber(adminTeacher);
//        System.out.println("i:"+i);
        return i;
    }


    /**
     * 更具老师ID修改老师信息
     * @param teacherName 老师名
     * @param jobNumber 工号
     * @param identity 身份证
     * @param gender 性别0女1男
     * @param phone 手机号
     * @param positionId 职位
     * @return
     */
    @Override
    public Page updateTeacherById(String teacherName, String jobNumber, String identity, Integer gender, String phone, Integer positionId,Integer id) {
        AdminTeacher teacher = new AdminTeacher();
        teacher.setTeacherName(teacherName);
        teacher.setJobNumber(jobNumber);
        teacher.setIdentity(identity);
        teacher.setGender(gender);
        teacher.setPhone(phone);
        teacher.setPositionId(positionId);

        teacher.setId(id);
        teacher.setSubmitTime(new Date());

        //判断是否重复
        int i = teacherDao.queryCountTeacherInfoByJobNumber(teacher);
        if (i > 0) {
            return Page.INFOERROR;
        }
        //继续执行修改
        else {
            int msg = teacherDao.updateTeacherById(teacher,id);
//        System.out.println(msg);
            if (msg > 0) {
                return Page.SUCCESS;
            } else {
                return Page.FAIL;
            }
        }
    }

    /**
     * 根据教师ID停用教师
     * @param id ID
     * @return
     */
    @Override
    public Page disableTeacherById(Integer id) {
        int i = teacherDao.disableTeacherById(id);
        if (i>0){
            return Page.SUCCESS;
        }else{
            return  Page.FAIL;
        }
    }


    /**
     * 根据教师ID启用教师
     * @param id ID
     * @return
     */
    @Override
    public Page ableTeacherById(Integer id) {
        int i = teacherDao.ableTeacherById(id);
        if (i>0){
            return Page.SUCCESS;
        }else{
            return  Page.FAIL;
        }
    }


    /**
     * 根据教师ID停用/启用教师
     * @param id ID
     * @param status 状态
     * @return
     */
    @Override
    public Page changeTeacherrById(Integer id, Integer status) {
        int msg = 0;
        //停用教师
        if (status == 1){
            msg = teacherDao.disableTeacherById(id);
        }
        //启用教师
        if (status == 0){
            msg = teacherDao.ableTeacherById(id);
        }
        if (msg>0){
            return Page.SUCCESS;
        }else{
            return  Page.FAIL;
        }
    }


    /**
     * 根据教师id删除教师 可批量删除
     * @param teacherIdList
     * @return
     */
    @Override
    public Page deleteTeacherById(List teacherIdList) {
        int i1 = 0;
        for (int i = 0;i<teacherIdList.size();i++){
            i1 = teacherDao.deleteTeacherById((Integer) teacherIdList.get(i));
            i1++;
        }
        if (i1>0){
            return Page.SUCCESS;
        }else{
            return Page.FAIL;
        }
    }


}
